1 00:00:00,470 --> 00:00:01,160 Hey there. 2 00:00:01,160 --> 00:00:04,430 In this lecture we're going to take a look at typecasting. 3 00:00:04,460 --> 00:00:09,320 The purpose of typecasting is to specify or convert the type of an expression. 4 00:00:09,320 --> 00:00:15,980 When that expression is too generic, and an expression is simply just a value or something that executes 5 00:00:15,980 --> 00:00:17,090 and returns a value. 6 00:00:17,090 --> 00:00:21,380 So this would include variables, parameters, and return values from functions. 7 00:00:21,380 --> 00:00:26,330 There may be times when an automatically inferred type is too generic, and you want to narrow it down 8 00:00:26,330 --> 00:00:27,950 to be a more specific type. 9 00:00:27,950 --> 00:00:30,830 And in order to do this, we have to use typecasting. 10 00:00:30,830 --> 00:00:36,590 However, in order to typecast the original type and the type of the typecast have to be related. 11 00:00:36,590 --> 00:00:40,400 So let's go ahead and take a look at some examples for typecasting. 12 00:00:40,640 --> 00:00:42,170 So I'm going to create a table. 13 00:00:42,170 --> 00:00:44,090 I'm just going to call it my table. 14 00:00:45,080 --> 00:00:47,150 And inside we're going to have a key value pair. 15 00:00:47,150 --> 00:00:48,170 I'll just call it names. 16 00:00:48,170 --> 00:00:50,450 And it's going to be equal to a table. 17 00:00:50,450 --> 00:00:56,120 Now let's say I wanted to be able to say that only this table can contain strings. 18 00:00:56,120 --> 00:00:59,420 I don't want to be able to insert anything else into this table. 19 00:00:59,420 --> 00:01:05,150 Well, in its current state, when I go to insert something into this table, like my table names and 20 00:01:05,150 --> 00:01:08,390 I can insert a number, we're not going to have any issues here. 21 00:01:08,390 --> 00:01:11,030 It's not going to underline anything red for us. 22 00:01:11,060 --> 00:01:16,130 However, if I copy this I'll call this my table two. 23 00:01:16,130 --> 00:01:22,790 And then I put a typecast on this table to make it only contain, um, values of a specific type. 24 00:01:22,790 --> 00:01:24,760 then we're going to get a red underline. 25 00:01:24,760 --> 00:01:27,640 If we try to insert a value that isn't of that type. 26 00:01:27,640 --> 00:01:32,470 So in order to create a type cast we need to use two colons instead of one. 27 00:01:32,470 --> 00:01:35,860 So this is not type annotation but this is type casting. 28 00:01:35,860 --> 00:01:38,920 So we do two colons after we assign the value. 29 00:01:38,920 --> 00:01:41,110 And then we can put a table in here. 30 00:01:41,110 --> 00:01:43,120 And inside this table we can define that. 31 00:01:43,120 --> 00:01:47,290 This names table is going to contain values of type string. 32 00:01:47,290 --> 00:01:50,560 And that means now our number one here is going to be underlined. 33 00:01:50,590 --> 00:01:53,860 Red type number could not be converted into string. 34 00:01:53,860 --> 00:01:56,470 And that's because we've explicitly defined here. 35 00:01:56,470 --> 00:02:01,480 Or we've cast the type of string to be contained within our names table. 36 00:02:02,050 --> 00:02:07,450 Now another important thing to note is that type casts are also automatically checked by the type engine, 37 00:02:07,450 --> 00:02:10,240 meaning you can't cast unrelated types. 38 00:02:10,240 --> 00:02:15,690 So as an example, if I created a variable, I'll call it sum num and assign it a number like one. 39 00:02:15,690 --> 00:02:17,850 And then I try to create another variable. 40 00:02:17,850 --> 00:02:21,000 I'll call it value one and set it equal to some numb. 41 00:02:21,000 --> 00:02:26,880 I wouldn't be able to do something like set it to a boolean or a string, because those types are unrelated. 42 00:02:26,880 --> 00:02:30,360 I could set it to something to be something more generic, like any. 43 00:02:30,360 --> 00:02:35,820 So if I do this with a typecast, this is okay, we're not going to get any red underline here. 44 00:02:35,820 --> 00:02:42,480 And now that means value one exists as any because we've casted it to be of type any from some num. 45 00:02:42,900 --> 00:02:44,760 But we can't do something like this. 46 00:02:44,760 --> 00:02:51,090 If I created a value, set it equal to some num, and then I try to cast the type of like boolean on 47 00:02:51,090 --> 00:02:51,840 it. 48 00:02:52,020 --> 00:02:54,030 We're going to get a red underline here. 49 00:02:54,030 --> 00:02:58,740 And that's because we cannot cast number into boolean because the types are unrelated. 50 00:02:58,740 --> 00:03:04,020 So again this is the type checking engine checking for us and seeing hey you can't do this. 51 00:03:04,020 --> 00:03:05,670 These types are unrelated. 52 00:03:05,670 --> 00:03:09,290 Now another example we're going to take a look at is this function right here. 53 00:03:09,290 --> 00:03:11,900 And you might remember this function from our previous lectures. 54 00:03:11,900 --> 00:03:15,170 So basically we had this function here that took two numbers. 55 00:03:15,170 --> 00:03:19,070 And one number was a and it has to be a number. 56 00:03:19,070 --> 00:03:20,750 And the other parameter was b. 57 00:03:20,750 --> 00:03:22,730 And this was an optional parameter. 58 00:03:22,730 --> 00:03:27,050 We don't know if uh whoever uses this function is going to pass a number to be or not. 59 00:03:27,050 --> 00:03:29,060 So we set a default right here. 60 00:03:29,060 --> 00:03:34,610 However, the type checking engine isn't smart enough to realize that we've set a default for B, so 61 00:03:34,610 --> 00:03:36,620 it says right here type number. 62 00:03:36,620 --> 00:03:39,470 Question mark could not be converted into number. 63 00:03:39,470 --> 00:03:41,660 So we're going to have this error here. 64 00:03:41,660 --> 00:03:45,200 And you might be wondering well how do we get rid of this error. 65 00:03:45,290 --> 00:03:52,220 Well we can use typecasting in order to fix this error because number question mark is an optional parameter, 66 00:03:52,220 --> 00:03:52,670 right. 67 00:03:52,670 --> 00:03:54,380 It is still of the type number. 68 00:03:54,380 --> 00:03:58,040 And that means number question mark and number are related types. 69 00:03:58,040 --> 00:04:00,950 That means we can cast B to be of type number. 70 00:04:00,950 --> 00:04:04,010 And that means this error here would disappear. 71 00:04:04,010 --> 00:04:11,120 So what I could do right here is I could put two colons and type cast number for B here for this return 72 00:04:11,120 --> 00:04:11,690 value. 73 00:04:11,690 --> 00:04:15,560 And now as you can see that red underline has completely disappeared. 74 00:04:15,920 --> 00:04:20,360 And this is because we've specified the type more clearly for the type checking engine. 75 00:04:20,360 --> 00:04:23,060 We've narrowed it down to be of the type number. 76 00:04:23,510 --> 00:04:26,410 Now these were just some basic examples of type casting. 77 00:04:26,410 --> 00:04:31,720 And you you probably won't use them that often, but they do exist to help to make your code more readable 78 00:04:31,720 --> 00:04:33,910 to both you and the type checking engine. 79 00:04:33,910 --> 00:04:36,580 Solving errors like the one we just did here. 80 00:04:37,090 --> 00:04:42,730 So again, you won't likely use type casting that often, but it is very useful in those situations 81 00:04:42,730 --> 00:04:45,280 where you have to use it like right here. 82 00:04:45,640 --> 00:04:50,440 Otherwise, I hope this lecture helped you out a little bit with type casting, and I'll see you in 83 00:04:50,440 --> 00:04:51,400 the next lecture.